//
//  tagVideoPlayerView.m
//  tagPlayerModule
//
//  Created by liqian on 2018/10/23.
//  Copyright © 2018年 liqian. All rights reserved.
//
#define ImageNameUrl(name) [NSString stringWithFormat:@"tagPlayericon.bundle/%@",name]
#import <AVFoundation/AVFoundation.h>
#import "tagVideoPlayerView.h"
#import "VideoPlayerController.h"
@interface tagVideoPlayerView ()


@property (nonatomic, strong) UIImageView *  iconImg;
@property (nonatomic, strong) UIButton    *  bofangBtn;
@end
@implementation tagVideoPlayerView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self videoInitView];
    }
    return self;
}


- (void)videoInitView{
    
    [self addSubview:self.iconImg];
    [self addSubview:self.bofangBtn];
}



/**
 点击播放

 @param sender 按钮点击事件
 */
- (void)bofangButtonClick:(UIButton *)sender{
    VideoPlayerController * video = [VideoPlayerController new];
    UIViewController * vc = [self findViewController:self];
    video.pathUrl = self.pathUrl;
    [vc presentViewController:video animated:YES completion:nil];
}

- (UIViewController *)findViewController:(UIView*)view{
  
    id responder = view;
    while (responder){
     if ([responder isKindOfClass:[UIViewController class]]){
         return responder;
        }
        responder = [responder nextResponder];
    }
    return nil;
}
/**
 接收音频地址信息
 
 @param pathUrl 链接
 */

- (void)setPathUrl:(NSString *)pathUrl{
    _pathUrl = pathUrl;
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            self.iconImg.image = [self thumbnailImageForVideo:[NSURL URLWithString:self.pathUrl] atTime:1];
        });
    });
}

- (UIImageView *)iconImg{
    if (!_iconImg) {
        
        _iconImg = [[UIImageView alloc] init];
        
    }
    return _iconImg;
}

- (UIButton *)bofangBtn{
    if (!_bofangBtn) {
        
        _bofangBtn = [[UIButton alloc] init];
        [_bofangBtn setImage:[UIImage imageNamed:ImageNameUrl(@"bofang_icon")] forState:UIControlStateNormal];
        [_bofangBtn addTarget:self action:@selector(bofangButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _bofangBtn;
}



/**
 刷新UI
 */
- (void)layoutSubviews{
    [super layoutSubviews];
    NSLog(@"%@",self);
    self.clipsToBounds = YES;
    self.iconImg.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    self.bofangBtn.bounds = CGRectMake(0, 0, 30, 30);
    self.bofangBtn.center = self.iconImg.center;
    
}

- (UIImage*) thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time {
    
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
    NSParameterAssert(asset);
    AVAssetImageGenerator *assetImageGenerator =[[AVAssetImageGenerator alloc] initWithAsset:asset];
    assetImageGenerator.appliesPreferredTrackTransform = YES;
    assetImageGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
    
    CGImageRef thumbnailImageRef = NULL;
    CFTimeInterval thumbnailImageTime = time;
    NSError *thumbnailImageGenerationError = nil;
    thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60)actualTime:NULL error:&thumbnailImageGenerationError];
    
    if(!thumbnailImageRef)
        NSLog(@"thumbnailImageGenerationError %@",thumbnailImageGenerationError);
    
    UIImage*thumbnailImage = thumbnailImageRef ? [[UIImage alloc]initWithCGImage: thumbnailImageRef] : nil;
    
    return thumbnailImage;
}

@end
